
var activeTimeout = null;
var reloadActiveTime = 5000;
var filter = {
	country: 0,
	city: 0,
	tour: 0
};

$(document).ready(function()
{
	loadActiveLot();
	
	$('#city-filter').autocomplete({
    	serviceUrl: '/reference/geo/complete',
 	    minChars: 2,
 	    delimiter: /(,|;)\s*/, // regex or character
 	    maxHeight: 400,
 	    width: 300,
 	    zIndex:  9999,
 	    deferRequestBy: 300, //miliseconds
 	    params: { level: 3},
 	    noCache: false,
 	    onSelect: function(value, data){
 	    	loadActiveLot({city: data});
 	    	if ( $('#city-filter').parent().find('.clear').length ) return;
 	    	$('#city-filter').parent().append('<a class="clear" href="javascript://">[x]</a>');
 	    	$('#city-filter').parent().find('.clear').click(function(){
 	    		$('#city-filter').val('');
 	    		$('#city-filter').parent().find('.clear').remove();
 	    		loadActiveLot({city: 0});
 	    	});
 	    }    	
    });
	
	$('#country-filter').autocomplete({
    	serviceUrl: '/reference/geo/complete',
 	    minChars: 2,
 	    delimiter: /(,|;)\s*/, // regex or character
 	    maxHeight: 400,
 	    width: 300,
 	    zIndex:  9999,
 	    deferRequestBy: 300, //miliseconds
 	    params: { level: 1 },
 	    noCache: false,
 	    onSelect: function(value, data){
 	    	loadActiveLot({country: data});
 	    	if ( $('#country-filter').parent().find('.clear').length ) return;
	 	    $('#country-filter').parent().append('<a class="clear" href="javascript://">[x]</a>');
	 	    $('#country-filter').parent().find('.clear').click(function(){
	 	    	$('#country-filter').val('');
	 	    	$('#country-filter').parent().find('.clear').remove();
	 	    	loadActiveLot({country: 0});
	 	    });
 	    }    	
    });


	$('#tour-filter').autocomplete({
    	serviceUrl: '/reference/tour/complete',
 	    minChars: 2,
 	    delimiter: /(,|;)\s*/, // regex or character
 	    maxHeight: 400,
 	    width: 300,
 	    zIndex:  9999,
 	    deferRequestBy: 300, //miliseconds
 	    params: {},
 	    noCache: false,
 	    onSelect: function(value, data){
 	    	loadActiveLot({tour: data});
 	    	if( $('#tour-filter').parent().find('.clear').length ) return;
	 	    $('#tour-filter').parent().append('<a class="clear" href="javascript://">[x]</a>');
	 	    $('#tour-filter').parent().find('.clear').click(function(){
	 	    	$('#tour-filter').val('');
	 	    	$('#tour-filter').parent().find('.clear').remove();
	 	    	loadActiveLot({tour: 0});
	 	    });
 	    }    	
    });
	
});

function loadActiveLot (opts) 
{
	filter = opts ? $.extend(filter, opts) : filter;
	data = {};
	for ( key in filter ) {
		if ( filter[key] ) {
			data[key] = filter[key]; 
		}
	}
	$.get('/tender/lot/active', data, function(data)
	{
		$('#active-lot tbody').empty();
		for( i = 0; i < data.length; i++ ) 
		{
			$('#active-lot tbody').append(
					'<tr bgcolor="'+ (data[i].is_owner ? '#EEE8AA' : '#DCDCDC' )+'">'+
					'<td>'+data[i].timeto+'</td>'+
					'<td>'+data[i].city+'</td>'+
					'<td>'+
						((data[i].tour.length) ? (
						(data[i].tour.length == 2 ? '<span style="color: green">'+data[i].tour[1].join(', ')+'</span><br/>' : '')+
						(data[i].tour.length >0 ? '<span style="color: red">'+data[i].tour[0].join(', ')+'</span><br/>' : '')
						) : '') +
					'</td>'+
					'<td>'+
						((data[i].country.length ) ? (
								(data[i].country.length == 2 ? '<span style="color: green">'+data[i].country[1].join(', ')+'</span><br/>' : '') +
								(data[i].country.length > 0 ? '<span style="color: red">'+data[i].country[0].join(', ')+'</span><br/>' : '' ) 
						) : '') +
					'</td>'+
					'<td>'+(data[i].first_price*1)+'</td>'+
					'<td>'+(data[i].minimal_price*1)+'</td>'+
					'<td>'+(data[i].offer_count*1)+'</td>'+
					'</tr>'
			);
		}
		if ( activeTimeout ) clearTimeout(activeTimeout);
		activeTimeout = setTimeout(function(){loadActiveLot();}, reloadActiveTime);
	});	
}